<?php 
	function update_condition($array){
		if(is_array($array)){
			$query="";
		}
	}
	function addItem($array){
		if(is_array($array)){
			$query = "INSERT INTO item (cat_id, item_name, item_serial, item_desc,item_status) VALUES ";
			$values = '';
			$duplicate_serial = 'Cannot insert the following due to duplicate serial in the database: <br/>';
			$results = array();
			$insert_ctr = 0;
			foreach($array as $v){
				if($v['category_id'] == '' || $v['name'] == '' || $v['serial'] == '' || $v['desc'] == ''){
					// do not insert					
				}
				else{
					if(!check_item($v['serial'])){
						$insert_ctr++;
						$values = $values."('".$v['category_id']."', '".$v['name']."', '".$v['serial']."', '".$v['desc']."', 'AVAILABLE'), ";
					}
					else {
						$flag = 1;
						$duplicate_serial = $duplicate_serial.$v['name'].'<br/>';	
					}
				}
			}
			$values = substr_replace($values, '', -2);
			$query = $query.$values;
			if($insert_ctr > 0){
				$r = mysql_query($query) or die('Error: '.mysql_error());
				if($r >= 1){
					$results['insert_status'] = 1;
				}
				else {
					$results['insert_status'] = 0;
				}
			}
			$results['inserted'] = $insert_ctr;
			if(isset($flag)){
				$results['duplicate'] = $duplicate_serial;
			}
			return $results;
		}
	}
	function add_item($item_name, $item_serial, $item_desc, $item_status){
	$insert="INSERT INTO item (`item_name`, `item_serial`, `item_desc`,`item_status`) VALUES ( '$item_name', '$item_serial', '$item_desc', 'AVAILABLE')";
	$query=mysql_query($insert) or die('ERROR:'.mysql_error());
	return $query >=1 ? TRUE: FALSE;
	}
	function check_item($item_serial){
		$check="SELECT * FROM item WHERE item_serial='$item_serial'";
		$b=mysql_query($check) or die('ERROR:'.mysql_error());
		return mysql_num_rows($b) >=1 ? TRUE: FALSE;
	}
	function check_user($user){
	$check="SELECT * FROM user WHERE user='$user'";
	$b=mysql_query($check) or die('ERROR:'.mysql_error());
		return mysql_num_rows($b) >=1 ? TRUE: FALSE;
	}
	function select_item(){
		$view="SELECT * FROM item LEFT JOIN category ON category.category_id = item.cat_id";
		$result=mysql_query($view) or die('ERROR:'.mysql_error());
		$results=array();
		if(mysql_num_rows($result)>=1){
			while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
				$results[]=$row;
			}
			return $results;
		}
		else
		return 0;
	}
	function select_item_id(){
		$selet_id= "SELECT * FROM item WHERE item_id='$item_id'";
		$result=mysql_query($selet_id) or die('ERROR:'.mysql_error());
				$results=array();
		if(mysql_num_rows($result)>=1){
			while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
				$results[]=$row;
			}
			return $results;
		}
		else
		return 0;
	}
	function pagination(){
	$per_page=10;
	$pages_query=mysql_query("SELECT count(`item_id`) from item");
	
	$page=(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
	$start=($page-1) * $per_page; 
	$sql=mysql_query("SELECT * FROM item LIMIT $start , $per_page"); 
	while($row=mysql_fetch_array($sql)){$pages= ceil(mysql_result($pages_query, 0)/ $per_page) ;
echo'';
}
echo'';
	if($page>=1 && $page <= $pages){
	for($x=1; $x<=$pages; $x++ ){
echo  ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a> </strong>' : '<a href="?page='.$x.'">'.$x.'</a>';
}}
echo'';  
}
	function getAvailableItems(){
		$query = "SELECT * FROM item WHERE item_status = 'AVAILABLE' ";	
		$r = mysql_query($query) or die('Error'.mysql_error());
		$results = array();
		
		if(mysql_num_rows($r) >= 1){
			while($row = mysql_fetch_array($r, MYSQL_ASSOC)){
				$results[] = $row;
			}
			return $results;
		}
		else
			return 0;
	}
	
	
	function getCategoryItems($cat_id){
		if($cat_id != 'all')
			$query = "SELECT * FROM item LEFT JOIN category ON category.category_id = item.cat_id WHERE item.cat_id = '$cat_id' AND item_status = 'AVAILABLE'";
		else
			$query = "SELECT * FROM item LEFT JOIN category ON category.category_id = item.cat_id WHERE item_status = 'AVAILABLE'";
			
		$r = mysql_query($query) or die('Error: '.mysql_error());
		if(mysql_num_rows($r) >= 1){
			$results = array();
			while($row = mysql_fetch_array($r,MYSQL_ASSOC)){
				$results[] = $row;
			}
			return $results;
		}
		else
			return 0;
	}
	
	function getSelectedItems($item_id_array){
		$query = "SELECT * FROM item WHERE item_id IN (".implode(',', $item_id_array).") ";
		$r = mysql_query($query) or die('Error: '.mysql_error());
		if(mysql_num_rows($r) >= 1){
			$results = array();
			while($row = mysql_fetch_array($r, MYSQL_ASSOC)){
				$results[] = $row;	
			}
			return $results;
		}
		else
			return 0;
	}
?>